/**
 * jims
 */
package com.jims.master.modules.operation.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.NumberUtils;
import com.jims.master.modules.doctor.dao.PatVisitDao;
import com.jims.master.modules.doctor.dao.PrescriptionApplyDao;
import com.jims.master.modules.doctor.dao.PrescriptionItemDao;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.entity.PrescriptionApply;
import com.jims.master.modules.doctor.entity.PrescriptionItem;
import com.jims.master.modules.operation.dao.OperationCostDao;
import com.jims.master.modules.operation.entity.OperationCost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 手术费用登记Service
 * @author ZhuQ
 * @version 2017-04-12
 */
@Service
@Transactional(readOnly = true)
public class OperationCostService extends CrudService<OperationCostDao, OperationCost> {

    @Autowired
    private PatVisitDao patVisitDao;
    @Autowired
    private PrescriptionApplyDao prescriptionApplyDao;
    @Autowired
    private PrescriptionItemDao prescriptionItemDao;



	public OperationCost get(String id) {
		return super.get(id);
	}
	
	public List<OperationCost> findList(OperationCost operationCost) {
		return super.findList(operationCost);
	}
	
	public Page<OperationCost> findPage(Page<OperationCost> page, OperationCost operationCost) {
		return super.findPage(page, operationCost);
	}

    /*
    *根据手术收费单生成领药的处方数据
    *ZhuQ
    * */
    @Transactional(readOnly = false)
    public int saveGenerate(List<OperationCost> operationCostList) {
        //根据operationCostList获取价表
        return super.save(operationCostList.get(0));
    }

    @Transactional(readOnly = false)
	public int save(OperationCost operationCost) {
		return super.save(operationCost);
	}
	
	@Transactional(readOnly = false)
	public int delete(OperationCost operationCost) {
		return super.delete(operationCost);
	}

    @Transactional(readOnly = false)
    public List<OperationCost> findByMasterId(OperationCost operationCost) {
        //获取List,若List长度为0，则将做的手术查出来并计费存进去再返回list，若list长度大于0，则直接获取list返回
        List<OperationCost> operationCostList=dao.findByMasterId(operationCost);
//        if(){}
        return operationCostList;
    }

    /**
     * 查询未生成领药单的术后划价药品项目
     * @param operationCost
     * @return
     */
    public List<OperationCost>findByPrescFlag (OperationCost operationCost){
        return dao.findByPrescFlag(operationCost);
    }

    @Transactional(readOnly = false)
    public int operationCostCreate(OperationCost operationCost){
        int num=1;
        //获取要生成领药单的list
        List<OperationCost> operationCostList = new ArrayList<>();
        operationCostList=dao.findByPrescFlag(operationCost);

        //循环list,生成不同的处方主表和详情表信息
        for(int i=0;i<operationCostList.size();i++){
            //判断这条信息和上条信息是否为同一个人
            PrescriptionApply prescriptionApply = new PrescriptionApply();
            OperationCost operationCostNew =new OperationCost();
            operationCostNew=dao.get(operationCostList.get(i).getId());
            if(i>0 && !operationCostList.get(i).getRemarks().equals(operationCostList.get(i-1).getRemarks()) || i==0){
                //根据visitId获取在住院人员信息
                PatVisit patVisit=patVisitDao.get(operationCostList.get(i).getRemarks());
                prescriptionApply.setPatientId(patVisit.getPatientId());
                prescriptionApply.setInoroutFlag("2");
                prescriptionApply.setVisitId(patVisit.getId());
                prescriptionApply.setApplyOn(NumberUtils.getVisitPrescription(prescriptionApply.getVisitId()));
                prescriptionApply.setConfirmFlag("0");
                prescriptionApply.setChargeFlag("0");
                prescriptionApply.setImplementDept(operationCostNew.getStorageId());
                prescriptionApply.setPrescAttr("1");
                prescriptionApply.setItemClass("A");
                prescriptionApply.setPrescStatus("0");
                prescriptionApply.setBillingDept(operationCostNew.getOrderDept());
                prescriptionApply.updateOrgId();
                prescriptionApply.preInsert();
                num=num*prescriptionApplyDao.insert(prescriptionApply);
            }
            //给处方字表准备数据
            PrescriptionItem prescriptionItem = new PrescriptionItem();
            prescriptionItem.setPriceId(operationCostNew.getClinicId());
            prescriptionItem=prescriptionItemDao.findByPriceId(prescriptionItem);
            prescriptionItem.setApplyId(prescriptionApply.getId());
            prescriptionItem.setChannel("3764148a89684ca8931c17e4f4074afd");
            prescriptionItem.setFrequency("F14EBAB8E80D4C4FA89E4B24EA3E4B65");
            prescriptionItem.setNum(operationCostNew.getAmount());
            prescriptionItem.setPrice(Double.valueOf(operationCostNew.getPrice()));
            prescriptionItem.setSumPrice(Double.valueOf(operationCostNew.getCost()));
            prescriptionItem.setIsSkin("0");
            prescriptionItem.preInsert();
            num=num*prescriptionItemDao.insert(prescriptionItem);
            operationCostNew.setPrescFlag("1");
            operationCostNew.preUpdate();
            num=num*dao.update(operationCostNew);
        }

        return num;
    }
}